package com.fengqilin.myapp;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;

public class CashierEntity {
    public static final String[] strings = new String[]{"C01", "C05", "C1", "Y1", "Y5", "Y10", "Y20", "Y50", "Y100"};
    MoneyEntity[] entities = new MoneyEntity[9];
    public MoneyEntity Coin0_1 = new MoneyEntity();

    public MoneyEntity Coin0_5 = new MoneyEntity();

    public MoneyEntity Coin1 = new MoneyEntity();

    public MoneyEntity m1 = new MoneyEntity();

    public MoneyEntity m10 = new MoneyEntity();

    public MoneyEntity m100 = new MoneyEntity();

    public MoneyEntity m20 = new MoneyEntity();

    public MoneyEntity m5 = new MoneyEntity();

    public MoneyEntity m50 = new MoneyEntity();

    public CashierEntity() {
        this.m100.money = 1000;
        this.m50.money = 500;
        this.m20.money = 200;
        this.m10.money = 100;
        this.m5.money = 50;
        this.m1.money = 10;
        this.Coin1.money = 10;
        this.Coin1.name += "硬币";
        this.Coin0_5.money = 5;
        this.Coin0_5.name += "硬币";
        this.Coin0_1.money = 1;
        this.Coin0_1.name += "硬币";
        entities[0] = this.Coin0_1;
        entities[1] = this.Coin0_5;
        entities[2] = this.Coin1;
        entities[3] = this.m1;
        entities[4] = this.m5;
        entities[5] = this.m10;
        entities[6] = this.m20;
        entities[7] = this.m50;
        entities[8] = this.m100;
    }

    public static CashierEntity json2Object(String str) {
        CashierEntity entity = new CashierEntity();
        try {
            JSONObject object = new JSONObject(str);
            for (int i = 0; i < entity.entities.length; i++) {
                String data = object.optString(CashierEntity.strings[i], "0");
                entity.entities[i].num = Integer.parseInt(data);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return entity;
    }

    public MoneyEntity getCoin0_1() {
        return Coin0_1;
    }

    public void setCoin0_1(MoneyEntity coin0_1) {
        Coin0_1 = coin0_1;
    }

    public MoneyEntity getCoin0_5() {
        return Coin0_5;
    }

    public void setCoin0_5(MoneyEntity coin0_5) {
        Coin0_5 = coin0_5;
    }

    public MoneyEntity getCoin1() {
        return Coin1;
    }

    public void setCoin1(MoneyEntity coin1) {
        Coin1 = coin1;
    }

    public MoneyEntity getM1() {
        return m1;
    }

    public void setM1(MoneyEntity m1) {
        this.m1 = m1;
    }

    public MoneyEntity getM10() {
        return m10;
    }

    public void setM10(MoneyEntity m10) {
        this.m10 = m10;
    }

    public MoneyEntity getM100() {
        return m100;
    }

    public void setM100(MoneyEntity m100) {
        this.m100 = m100;
    }

    public MoneyEntity getM20() {
        return m20;
    }

    public void setM20(MoneyEntity m20) {
        this.m20 = m20;
    }

    public MoneyEntity getM5() {
        return m5;
    }

    public void setM5(MoneyEntity m5) {
        this.m5 = m5;
    }

    public MoneyEntity getM50() {
        return m50;
    }

    public void setM50(MoneyEntity m50) {
        this.m50 = m50;
    }

    public int all() {
        return this.Coin0_1.sum()
                + this.Coin0_5.sum()
                + this.Coin1.sum()
                + this.m1.sum()
                + this.m5.sum()
                + this.m10.sum()
                + this.m20.sum()
                + this.m50.sum()
                + this.m100.sum();
    }

    @Override
    public String toString() {
        return "CashierEntity{" +
                "entities=" + Arrays.toString(entities) +
                ", Coin0_1=" + Coin0_1.toString() +
                ", Coin0_5=" + Coin0_5.toString() +
                ", Coin1=" + Coin1.toString() +
                ", m1=" + m1.toString() +
                ", m10=" + m10.toString() +
                ", m100=" + m100.toString() +
                ", m20=" + m20.toString() +
                ", m5=" + m5.toString() +
                ", m50=" + m50.toString() +
                '}';
    }

    public static class MoneyEntity {
        public int money;
        public String name = "元";

        public int num;

        public int shangjiao;

        public int sum() {
            return this.money * this.num;
        }

        public int isCheck(int paramInt) {
            this.shangjiao = shangJiaoMoney(paramInt);
            return paramInt - this.shangjiao * this.money;
        }

        private int shangJiaoMoney(int paramInt) {
            paramInt /= this.money;
            return (paramInt != 0) ? (Math.min(paramInt, this.num)) : 0;
        }

        @Override
        public String toString() {
            return "MoneyEntity{" +
                    "money=" + money +
                    ", name='" + name + '\'' +
                    ", num=" + num +
                    ", shangjiao=" + shangjiao +
                    '}';
        }
    }

    public int sunChange() {
        int j = all() - 10000;
        if (j < 0)
            return j;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("sunChange: number > ");
        stringBuilder.append(j);
        Log.i("MainActivity", stringBuilder.toString());
        for (MoneyEntity entity : this.entities) {
            j = entity.isCheck(j);
            if (j == 0) return 0;
        }
        return j;
    }
}
